package pckg.realRobot.uC;

import pckg.realRobot.Motor;
import pckg.realRobot.Signal;
import pckg.realRobot.SignalChange;


public class Port {

	private Signal value = Signal.LOW;
	
	private Signal prevValue = Signal.LOW;
	
	private SignalChange change = SignalChange.NOCHANGE;
	
	private Motor motor;
	
	public void setValue(Signal s, long timeStamp){
		prevValue = value;
		value = s;
		
		if (prevValue == value){
			change = SignalChange.NOCHANGE;
		} else if (prevValue==Signal.HIGH && value == Signal.LOW){
			change = SignalChange.DESCEND;
			changed(timeStamp);
		} else {
			change = SignalChange.ASCEND;
			changed(timeStamp);
		}
	}
	
	public void invert(long timeStamp){
		if (value == Signal.LOW){
			setValue(Signal.HIGH, timeStamp);
		} else {
			setValue(Signal.LOW, timeStamp);
		}
	}
	
	private void changed(long timeStamp){
		if (change == SignalChange.ASCEND){
			motor.move(timeStamp);
		}
	}
	
	public Signal getValue(){
		return value;	
	}
	
	public SignalChange getChange(){
		return change;
	}
	
	public Port(Motor m){
		motor = m;
	}

	public void reset() {
		value = Signal.LOW;
		prevValue = Signal.LOW;
		
		change = SignalChange.NOCHANGE;
	}
}
